package com.example.avmanagerweb.service.impl;

import com.example.avmanagerweb.Utils.MD5Util.MD5EncryptionExample;
import com.example.avmanagerweb.entity.Devicinpt;
import com.example.avmanagerweb.entity.SysDepartment;
import com.example.avmanagerweb.entity.SysDevic;
import com.example.avmanagerweb.entity.SysUserinfo;
import com.example.avmanagerweb.entity.vo.RemoveDevicVo;
import com.example.avmanagerweb.entity.vo.SysDevicVo;
import com.example.avmanagerweb.mapper.StaffdepartmentMapper;
import com.example.avmanagerweb.mapper.SysDevicMapper;
import com.example.avmanagerweb.result.exceptionhandler.JWException;
import com.example.avmanagerweb.service.DevicinptService;
import com.example.avmanagerweb.service.SysDevicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.avmanagerweb.service.SysUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2023-06-29
 */
@Service
public class SysDevicServiceImpl extends ServiceImpl<SysDevicMapper, SysDevic> implements SysDevicService {
    @Autowired
    private SysUserinfoService sysUserinfoService;
    @Autowired
    private DevicinptService devicinptService;
    @Resource
    private SysDevicMapper sysDevicMapper;
    @Override
    @Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT)        //事务
    public boolean addDevic(SysDevicVo sysDevicVo) {
        //设备参数
        SysDevic sysDevic = new SysDevic();
        sysDevic.setDvnum(sysDevicVo.getDvNum());          //设备编号
        sysDevic.setDvname(sysDevicVo.getDvName());        //名称
        sysDevic.setDvdepartmentid(sysDevicVo.getDvDepartmentID());     //部门ID
        sysDevic.setDvphoneno(sysDevicVo.getuPhone());      //手机号 可为空
        sysDevic.setDvcarno(sysDevicVo.getDvCarNo());      //车牌号 可为空
        sysDevic.setDvfenceid(sysDevicVo.getDvFenceID());   //围栏id 可为空
        sysDevic.setDvvideoserverid(sysDevicVo.getDvVideoServerID());   //视频服务器id 默认34
        sysDevic.setDvdevtype(sysDevicVo.getDvDevType());   //设备类型 默认2
        sysDevic.setDvobdserverid(sysDevicVo.getDvOBDServerID());   //终端运行服务器id 默认33
        sysDevic.setDvimei(sysDevicVo.getDvIMEI());   //IMEI号
        sysDevic.setDvrecordvideo(sysDevicVo.isDvRecordVideo());   //后台是否录像 默认0
        sysDevic.setDvusefence(sysDevicVo.isDvUseFence());   //是否使用围栏 默认0
        sysDevic.setDvoutfencealarm(sysDevicVo.isDvOutFenceAlarm());   //离开围栏报警 默认0
        sysDevic.setDvlaststatus(false);   //在线状态

        boolean save = this.save(sysDevic);
        if(save && sysDevicVo.isAddUser()){
            //用户参数
            SysUserinfo sysUserinfo = new SysUserinfo();
            sysUserinfo.setUdepid(sysDevicVo.getDvDepartmentID());      //部门ID
            sysUserinfo.setUloginname(sysDevicVo.getDvNum());      //登录名
            sysUserinfo.setUpwd(MD5EncryptionExample.encryptMD5("123456"));      //密码
            sysUserinfo.setUname(sysDevicVo.getDvName());   //用户名称
            sysUserinfo.setUphone(sysDevicVo.getuPhone());  //用户手机号
            sysUserinfo.setUgender(true);   //类型 管理员(单账号)为 0 绑定设备为1
            sysUserinfo.setUdvid(sysDevic.getDvid());       //与设备绑定时使用 绑定设备的id
            sysUserinfo.setUpolno(sysDevicVo.getDvNum());   //用户编号
            boolean save1 = sysUserinfoService.save(sysUserinfo);
            if(save1){
                return true;
            } else {
                throw new JWException(500,"添加异常");
            }
        }

        if(save) {
            return true;
        } else {
            return false;
        }



    }

    @Override
    @Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT)        //事务
    public boolean removeDevic(int id) {


        Devicinpt byId = devicinptService.getById(id);
        if(byId == null){
            return false;
        }
        Integer devicid = byId.getDevicid();
        Integer devicuserid = byId.getDevicuserid();

        boolean b = this.removeById(devicid);
        if(b){
            if(devicuserid == null || devicuserid == 0){
                return true;
            }
            boolean b1 = sysUserinfoService.removeById(devicuserid);
            if(b1){
                return true;
            } else {
                throw new JWException(500,"删除异常");
            }
        } else {
            throw new JWException(500,"删除异常");
        }
    }

    @Override
    @Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT)
    public boolean delDevList(List<Devicinpt> devicinpts, boolean fla) {
        List<Integer> userId = new ArrayList<>();
        for (Devicinpt devicinpt : devicinpts) {
            if(devicinpt.getDevicid() != 0){
                boolean b = this.removeById(devicinpt.getDevicid());
                if(!b){
//                    throw new JWException(500,"删除异常");
                }
            }
            if(devicinpt.getDevicuserid() != null && devicinpt.getDevicuserid() != 0){
                userId.add(devicinpt.getDevicuserid());
            }

        }
        if(fla && userId.size()>0){
            boolean b1 = sysUserinfoService.removeByIds(userId);
            if(b1){
                return true;
            } else {
                throw new JWException(500,"删除异常");
            }
        }

        return true;

    }

    /**
     *
     * @param devicinpts  设备
     * @param sysDepartments  部门
     * @param fla
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT)
    public boolean delDevList(List<Devicinpt> devicinpts, List<SysDepartment> sysDepartments, boolean fla) {
        boolean judge = true;
        List<Integer> userId = new ArrayList<>();
        for (Devicinpt devicinpt : devicinpts) {
            judge = true;
            for (SysDepartment sysDepartment : sysDepartments) {
                if(devicinpt.getDepartmentid() == sysDepartment.getDepid()){
                    judge = false;
                    break;
                }
            }
            if(judge) {
                throw new JWException(500,"批量删除 禁止跨组删除");
            }
            if(devicinpt.getDevicuserid() != null && devicinpt.getDevicuserid() != 0){
                userId.add(devicinpt.getDevicuserid());
            }
            boolean b = this.removeId(devicinpt.getDevicid());
            System.out.println(b);
        }


        if(fla && userId.size()>0){
            boolean b1 = sysUserinfoService.removeByIds(userId);
            if(b1){
                return true;
            } else {
                throw new JWException(500,"删除异常");
            }
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT)
    public boolean saveDevicLisr(SysDevicVo sysDevicVo) {
        for (int i = 0; i < sysDevicVo.getDevicNumber(); i++) {
            SysDevic sysDevic = new SysDevic();
            sysDevic.setDvnum(sysDevicVo.getDvNum()+""+i);          //设备编号
            sysDevic.setDvname(sysDevicVo.getDvNum()+""+i);        //名称
            sysDevic.setDvdepartmentid(sysDevicVo.getDvDepartmentID());     //部门ID
            sysDevic.setDvphoneno(sysDevicVo.getuPhone());      //手机号 可为空
            sysDevic.setDvcarno(sysDevicVo.getDvCarNo());      //车牌号 可为空
            sysDevic.setDvfenceid(sysDevicVo.getDvFenceID());   //围栏id 可为空
            sysDevic.setDvvideoserverid(sysDevicVo.getDvVideoServerID());   //视频服务器id 默认34
            sysDevic.setDvdevtype(sysDevicVo.getDvDevType());   //设备类型 默认2
            sysDevic.setDvobdserverid(sysDevicVo.getDvOBDServerID());   //终端运行服务器id 默认33
            sysDevic.setDvimei(sysDevicVo.getDvIMEI());   //IMEI号
            sysDevic.setDvrecordvideo(sysDevicVo.isDvRecordVideo());   //后台是否录像 默认0
            sysDevic.setDvusefence(sysDevicVo.isDvUseFence());   //是否使用围栏 默认0
            sysDevic.setDvoutfencealarm(sysDevicVo.isDvOutFenceAlarm());   //离开围栏报警 默认0
            sysDevic.setDvlaststatus(false);   //在线状态

            boolean b = this.save(sysDevic);
            if(!b){
                throw new JWException(500, "批量添加异常");
            }
            if(sysDevicVo.isAddUser()){
                SysUserinfo sysUserinfo = new SysUserinfo();
                sysUserinfo.setUdepid(sysDevicVo.getDvDepartmentID());      //部门ID
                sysUserinfo.setUloginname(sysDevicVo.getDvNum()+""+i);      //登录名
                sysUserinfo.setUpwd(MD5EncryptionExample.encryptMD5("123456"));      //密码
                sysUserinfo.setUname(sysDevicVo.getDvName());   //用户名称
                sysUserinfo.setUphone(sysDevicVo.getuPhone());  //用户手机号
                sysUserinfo.setUgender(true);   //类型 管理员(单账号)为 0 绑定设备为1
                sysUserinfo.setUdvid(sysDevic.getDvid());       //与设备绑定时使用 绑定设备的id
                sysUserinfo.setUpolno(sysDevicVo.getDvNum()+""+i);   //用户编号
                sysUserinfo.setUusertype(3);
                boolean b1 = sysUserinfoService.save(sysUserinfo);
                if(!b1){
                    throw new JWException(500, "批量添加异常");
                }
            }

        }
        
        return true;
    }

    @Override
    public boolean removeId(long id) {
        boolean b = sysDevicMapper.removeId(id);
        return b;
    }
}
